package appapi.controller.oider;

import appapi.Request.Basic.DelProjReq;
import appapi.Request.OperPageReq;
import appapi.Request.ProjPageReq;
import appapi.Global.BusinessException;
import appapi.Vo.AreaVo;
import appapi.Request.ProjReq;
import appapi.common.BusinessTypeEnum;
import appapi.common.ResponseBean;
import appapi.config.MyLog;
import appapi.config.CurrentUser;
import appapi.dto.AddProjectDto;
import appapi.dto.DelOperDto;
import appapi.dto.UpdateProjectDto;
import appapi.dto.basic.GetAreaDto;
import appapi.entity.Zhongjiao.*;
import appapi.entity.basic.UserInfo;
import appapi.service.*;
import appapi.utils.ExcelUtils;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author djq
 * @date 2023/5/11 17:04
 * @description:
 */

@Api(tags = "基础数据-项目信息、操作员")
@RestController
@RequestMapping(value = "/api/basicSet")
public class BasicSetController {
    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
    @Resource
    private IProjectService iProjectService;
    @Resource
    private IOperatorService iOperatorService;
    @Resource
    private IAreaService iAreaService;
    @Resource
    private IEquipmentMaintenanService iEquipmentMaintenanService;
    @Resource
    private IStaffService iStaffService;

    /**
     * 项目信息的分页查询
     *
     * @return
     * @auther lc 2023/5/12
     */
    @ApiOperation(value = "项目信息的分页查询", notes = "项目信息的分页查询")
    @GetMapping(value = "/GetProjectListByPage")
    public ResponseBean GetProjectListByPage(@CurrentUser UserInfo user, @Valid ProjPageReq projReq) {
            try {
                if (projReq.getCurrPage() == null || projReq.getPageSize() == null) {
                    projReq.setCurrPage(1);
                    projReq.setPageSize(10);
                }
                if (projReq.getOrgaPathIds() == null) {
                    //获取人员组织机构
                    Map<String, Object> staffDto = iStaffService.SelectStaffDetail(user.getUserid());
                    String orgaPathIds = staffDto.get("orgaPathIds").toString();
                    projReq.setOrgaPathIds(orgaPathIds);
                }
                List<Map<String, Object>> projects = iProjectService.SelectProjectPage(projReq);

                PageInfo pageInfo = new PageInfo(projects, projReq.getPageSize());

                return ResponseBean.success(pageInfo);
            } catch (Exception e) {
                return ResponseBean.fail("项目信息的分页异常");
            }
        }
        /**
         * 项目信息导出
         *
         * @return
         * @auther ygp 2023/6/12
         */
        @ApiOperation(value = "项目信息导出", notes = "项目信息导出接口", produces = "application/octet-stream")
        @GetMapping(value = "/ProjectRecExport")
        @ApiOperationSupport(ignoreParameters = {"currPage", "pageSize"})
        public ResponseBean ProjectRecExport (@CurrentUser UserInfo user, ProjPageReq req, HttpServletResponse response)
        {
            try {
                if (req.getOrgaPathIds() == null) {
                    //获取人员组织机构
                    Map<String, Object> staffDto = iStaffService.SelectStaffDetail(user.getUserid());
                    String orgaPathIds = staffDto.get("orgaPathIds").toString();
                    req.setOrgaPathIds(orgaPathIds);
                }
                List<ExcelProjectDto> list = iProjectService.getList(req);
                if (list.size() == 0) {
                    return ResponseBean.success("数据源为空");
                } else if (list.size() > 5000) {
                    return ResponseBean.fail("记录超过5000，请重新选择查询条件");
                } else {
                    ExcelUtils.exportExcel(list, "项目信息", "项目信息sheet", ExcelProjectDto.class, "项目信息", response);
                    return ResponseBean.success();
                }
            } catch (Exception e) {
                return ResponseBean.fail("项目信息导出异常");
            }
        }

        /**
         * 获取项目选择列表
         *
         * @return
         * @auther lc 2023/5/12
         */
        @ApiOperation(value = "获取项目选择列表", notes = "获取项目选择列表")
        @GetMapping(value = "/GetProjectList")
        public ResponseBean GetProjectList (@CurrentUser UserInfo user, @Valid ProjReq req){
            try {
                if (req.getOrgaPathIds() == null) {
                    //获取人员组织机构
                    Map<String, Object> staffDto = iStaffService.SelectStaffDetail(user.getUserid());
                    String orgaPathIds = staffDto.get("orgaPathIds").toString();
                    req.setOrgaPathIds(orgaPathIds);
                }
                List<Map<String, Object>> projList = iProjectService.getProjectList(req);
                return ResponseBean.success(projList);
            } catch (Exception e) {
                logger.error("获取项目选择列表异常", e);
                return ResponseBean.fail("获取项目选择列表异常");
            }
        }


        /**
         * 获取项目详情
         *
         * @return
         * @auther 2023/5/12
         */
        @ApiOperation(value = "获取项目详情", notes = "获取项目详情")
        @GetMapping(value = "/GetProjectById")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "query", name = "projId", dataType = "long", value = "项目Id")
        })
        public ResponseBean GetProjectById (Long projId){
            try {
                Map<String, Object> map = iProjectService.SelectProjectDetail(projId);

                return ResponseBean.success(map);
            } catch (BusinessException exception) {
                return ResponseBean.fail("获取项目详情异常: ".concat(exception.getMessage()));
            }
        }

        /**
         * 添加项目信息
         *
         * @return
         * @auther 2023/5/12
         */
        @ApiOperation(value = "添加项目信息", notes = "添加项目信息")
        @PostMapping(value = "/AddProject")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "formData", name = "projectDto", dataType = "object", value = "项目参数")
        })
        @MyLog(title = "添加项目信息", businessType = BusinessTypeEnum.insert)
        public ResponseBean AddProject (@Validated @RequestBody AddProjectDto projectDto){
            try {
                if (iProjectService.InsertProject(projectDto) == 0) {
                    return ResponseBean.fail("数据写入失败");
                }
                return ResponseBean.success();
            } catch (BusinessException exception) {

                return ResponseBean.fail("添加项目异常: ".concat(exception.getMessage()));

            }
        }

        /**
         * 编辑项目信息
         *
         * @return
         * @auther 2023/5/12
         */
        @ApiOperation(value = "编辑项目信息", notes = "编辑项目信息")
        @PostMapping(value = "/updateProject")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "formData", name = "projectDto", dataType = "object", value = "项目参数")
        })
        @MyLog(title = "编辑项目信息", businessType = BusinessTypeEnum.update)
        public ResponseBean updateProject (@Validated @RequestBody UpdateProjectDto projectDto){
            try {
                if (iProjectService.UpdateProject(projectDto) == 0) {
                    return ResponseBean.fail("更新数据失败");
                }
                return ResponseBean.updateSuccess();
            } catch (BusinessException exception) {

                return ResponseBean.fail("编辑项目信息异常: ".concat(exception.getMessage()));
            }
        }

        /**
         * 删除项目信息
         *
         * @return
         * @auther lc 2023/5/11
         */
        @ApiOperation(value = "批量删除项目信息", notes = "批量删除项目信息")
        @PostMapping(value = "/delProject")
        @MyLog(title = "删除项目信息", businessType = BusinessTypeEnum.delete)
        public ResponseBean delProject (@RequestBody DelProjReq req){
            try {
                if (iProjectService.DelProject(req.getProjId()) == 0) {
                    return ResponseBean.fail("删除数据失败");
                }
                return ResponseBean.success();
            } catch (BusinessException exception) {
                return ResponseBean.fail("删除失败: ".concat(exception.getMessage()));
            } catch (Exception exception) {
                return ResponseBean.fail("删除项目信息异常: ".concat(exception.getMessage()));
            }
        }

        //endregion

        //region 操作员信息

        /**
         * 分页获取操作员列表
         *
         * @return
         * @auther lc 2023/5/12
         */
        @ApiOperation(value = "分页获取操作员", notes = "分页获取操作员")
        @GetMapping(value = "/GetOperatorListByPage")
        public ResponseBean GetOperatorListByPage (@CurrentUser UserInfo user, OperPageReq operPageReq){
            try {
                if (operPageReq.getCurrPage() == null || operPageReq.getPageSize() == null) {
                    operPageReq.setCurrPage(1);
                    operPageReq.setPageSize(10);
                }
                if (operPageReq.getOrgaPathIds() == null) {
                    //获取人员组织机构
                    Map<String, Object> staffDto = iStaffService.SelectStaffDetail(user.getUserid());
                    String orgaPathIds = staffDto.get("orgaPathIds").toString();
                    operPageReq.setOrgaPathIds(orgaPathIds);
                }
                List<Map<String, Object>> list = iOperatorService.SelectOperatorPage(operPageReq);
                PageInfo pageInfo = new PageInfo(list, operPageReq.getPageSize());

                return ResponseBean.success(pageInfo);
            } catch (BusinessException exception) {
                return ResponseBean.fail("分页获取操作员分页异常: ".concat(exception.getMessage()));
            }
        }

        /**
         * 分页获取操作员列表
         *
         * @return
         * @auther lc 2023/5/12
         */
        @ApiOperation(value = "在设备下获取操作员列表", notes = "获取操作员列表")
        @GetMapping(value = "/GetOperatorList")
        public ResponseBean GetOperatorListBy (@CurrentUser UserInfo user, OperPageReq operPageReq){
            try {
                if (operPageReq.getOrgaPathIds() == null) {
                    //获取人员组织机构
                    Map<String, Object> staffDto = iStaffService.SelectStaffDetail(user.getUserid());
                    String orgaPathIds = staffDto.get("orgaPathIds").toString();
                    operPageReq.setOrgaPathIds(orgaPathIds);
                }
                List<OperatorDto> list = iOperatorService.SelectOperatorList(operPageReq);

                return ResponseBean.success(list);
            } catch (BusinessException exception) {
                return ResponseBean.fail("分页获取操作员列表异常: ".concat(exception.getMessage()));
            }
        }

        /**
         * 获取操作员详情
         *
         * @return
         * @auther 2023/5/12
         */
        @ApiOperation(value = "获取操作员详情", notes = "获取操作员详情")
        @GetMapping(value = "/GetOperatorById")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "query", name = "operId", dataType = "long", value = "管理人员Id")
        })
        public ResponseBean GetOperatorById (Long operId){
            try {
                Map<String, Object> map = iOperatorService.SelectOperatorDetail(operId);

                return ResponseBean.success(map);
            } catch (BusinessException exception) {
                return ResponseBean.fail("获取操作员详情异常: ".concat(exception.getMessage()));
            }
        }


        /**
         * 添加操作员信息
         *
         * @return
         * @auther 2023/5/12
         */
        @ApiOperation(value = "添加操作员信息", notes = "添加操作员信息")
        @PostMapping(value = "/AddOperator")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "formData", name = "operatorDto", dataType = "object", value = "操作员参数")
        })
        @MyLog(title = "添加操作员信息", businessType = BusinessTypeEnum.insert)
        public ResponseBean AddOperator (@Validated @RequestBody OperatorDto operatorDto){
            try {
                iOperatorService.InsertOperator(operatorDto);
                return ResponseBean.success();
            } catch (BusinessException exception) {
                return ResponseBean.fail("添加操作员信息异常: ".concat(exception.getMessage()));
            }
        }

        /**
         * 编辑操作员
         *
         * @return
         * @auther 2023/5/12
         */
        @ApiOperation(value = "编辑操作员", notes = "编辑操作员")
        @PostMapping(value = "/EditOperator")
        @MyLog(title = "编辑操作员信息", businessType = BusinessTypeEnum.update)
        public ResponseBean EditOperator (@Validated @RequestBody OperatorDto operatorDto){
            try {
                iOperatorService.UpdateOperator(operatorDto);
                return ResponseBean.success();
            } catch (BusinessException exception) {

                return ResponseBean.fail("编辑操作员异常: ".concat(exception.getMessage()));
            }
        }

        /**
         * 删除操作员信息
         *
         * @return
         * @auther djq 2023/5/11
         */
        @ApiOperation(value = "删除操作员信息", notes = "删除操作员信息")
        @PostMapping(value = "/DeleteOperator")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "formData", name = "dto", dataType = "object", value = "操作员id")
        })
        @MyLog(title = "删除操作员信息", businessType = BusinessTypeEnum.delete)
        public ResponseBean DeleteOperator (@RequestBody DelOperDto dto){
            try {
                iOperatorService.DelOperator(dto);
                return ResponseBean.success();
            } catch (BusinessException exception) {
                return ResponseBean.fail("删除操作员信息异常: ".concat(exception.getMessage()));
            }
        }


    //endregion

    //region 省市区

    /**
     * 省市区列表
     *
     * @return
     * @auther djq 2023/5/23
     */
    @ApiOperation(value = "省市区列表", notes = "省市区列表")
    @GetMapping(value = "/AreaList")
    public ResponseBean AreaList() {
        try {

            List<AreaVo> list = iAreaService.SelectList();

            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("省市区列表异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 省市区懒加载
     *
     * @return
     * @auther djq 2023/5/23
     */
    @ApiOperation(value = "省市区懒加载", notes = "省市区懒加载")
    @GetMapping(value = "/GetAreaList")
    @ApiImplicitParam(paramType = "formData", name = "dto", dataType = "object", value = "查询参数")
    public ResponseBean GetAreaList(GetAreaDto dto) {
        try {

            List<AreaVo> list = iAreaService.SelectList(dto);
            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("省市区列表异常: ".concat(exception.getMessage()));
        }
    }


    //endregion

    /**
     * 新增维保项目
     *
     * @return
     * @auther djq 2023/5/25
     */
    @ApiOperation(value = "新增维保项目", notes = "新增维保项目")
    @PostMapping(value = "/AddMaintenanItem")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "dto", dataType = "object", value = "设备参数")
    })
    @MyLog(title = "新增维保项目",businessType = BusinessTypeEnum.insert)
    public ResponseBean AddMaintenanItem(@Validated @RequestBody MaintenanitemDto dto) {
        try {
            iEquipmentMaintenanService.InsertMaintenanItem(dto);
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return ResponseBean.fail("新增维保异常: ".concat(exception.getMessage()));
        }
    }
}
